package com.camp.android.trip.n.track;

import java.text.SimpleDateFormat;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v4.app.ShareCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.camp.android.trip.n.track.R;

 

public class ItemActivity extends Activity {
	
	public int itemid;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();
        itemid = Integer.parseInt(intent.getStringExtra("item_id"));
        // set text
        
        TextView date_time = (TextView) findViewById(R.id.date_time);
        
        Log.i("ItemActivity","Loading ItemActivity with id= "+itemid);
        DatabaseManager dbm = new DatabaseManager(this);
        Item item = dbm.getItem(itemid);
        setTitle(item.name);
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        String startStr = dateFormat.format(item.start.getTime());
        String endStr = dateFormat.format(item.end.getTime());
        date_time.setText(startStr + " to " + endStr);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_item, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
            case R.id.tab_add_location:
            	AlertDialog.Builder alert = new AlertDialog.Builder(this);                 
            	alert.setTitle("Add a location");  
            	alert.setMessage("Enter City, Country, etc. :");                

            	 // Set an EditText view to get user input   
            	 final EditText input = new EditText(this); 
            	 alert.setView(input);
            	    alert.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {  
            	       public void onClick(DialogInterface dialog, int which) {
            	            // TODO Auto-generated method stub
            	            return;      
            	            
            	    }  
            	     });  

            	    alert.setNegativeButton("Add", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {  
            	        String location_value = input.getText().toString();
            	        Button location_button= (Button)findViewById(R.id.add_location_button);
            	    	location_button.setText(location_value);
            	    	location_button.setVisibility(0);
            	    	
            	        return;                      	         
            	        }
            	    });
            	            alert.show();
            case R.id.tab_add_photo:	
			Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
			 startActivityForResult(intent, 0); 
			 return true;
            case R.id.tab_add_video:
            	Intent i = new Intent();
            	i.setAction(Intent.ACTION_PICK);
            	i.setDataAndType(Uri.parse("content://media/external/images/media"), "image/*");
            	        startActivityForResult(i, 0); 
            	        return true;
            case R.id.tab_add_alarm:
            	// TODO set alarm for before start time
		}
        
        return super.onOptionsItemSelected(item);
    }
    
    public void findOnMap(View view) {
        String action = Intent.ACTION_VIEW;
        Button location_button= (Button)findViewById(R.id.add_location_button);
        Uri uri = Uri.parse("geo:0,0?q=" + location_button.getText());
        Intent intent = new Intent(action, uri);
        startActivity(intent);
    }
    //Dialog button add location
    public void addLocation(View view){
    	EditText location = (EditText)findViewById(R.id.enter_location);
    	LinearLayout itemLayout = (LinearLayout)findViewById(R.id.item_main_layout);
    	Button location_button= new Button(this);
    	itemLayout.addView(location_button);
    }
    

}
